package cn.wp.ewallet;

import cn.wp.common.core.Wp;
import cn.wp.common.core.plugin.scheduler.SchedulerService;
import cn.wp.common.core.plugin.scheduler.SchedulerTask;
import cn.wp.common.core.startup.Startup;
import cn.wp.common.core.util.ApplicationContextUtil;
import cn.wp.ewallet.wallet.scheduler.*;
import cn.wp.ewallet.wmps.scheduler.ProdOrderRelOrderStatusChangeRunnable;
import cn.wp.ewallet.wallet.service.AutoCollectService;
import cn.wp.ewallet.wmps.scheduler.RegularSettlementRunnable;
import cn.wp.ewallet.wmps.scheduler.TtySettlementRunnable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 定时器自动启动类
 * Created by chenyb on 2019/10/23
 */
public class EwalletSchedulerStartup implements Startup {
    Logger logger = LoggerFactory.getLogger(EwalletSchedulerStartup.class);

    @Override
    public void start(Wp wp) {
        System.err.println("定时器启动类");
        SchedulerService schedulerService = ApplicationContextUtil.getBean(SchedulerService.class);
        //定期理财日结任务
        regularSettlememtTask(schedulerService);

        //自动归集
        autoCollectTask(schedulerService);

        //交易状态确认
        transferStatusConfirmTask(schedulerService);

        //天天盈理财日结任务
        ttySettlementTask(schedulerService);

        /**
         * 修改理财定期状态
         */
        prodOrderRelOrderStatusTask(schedulerService);

        //同步账户资产信息
        syncAssetsTask(schedulerService);

        //手续费统计
        feeStatisticesTask(schedulerService);

        //清理超时订单
        clearTimeoutUserOrder(schedulerService);

    }


    private void prodOrderRelOrderStatusTask(SchedulerService schedulerService) {
        SchedulerTask prodOrderRelOrderStatusChangeTask= new SchedulerTask();
        prodOrderRelOrderStatusChangeTask.setCron("0 0 8 * * ?");
        prodOrderRelOrderStatusChangeTask.setTaskId("1");
        prodOrderRelOrderStatusChangeTask.setTaskName("修改理财产品订单状态定时任务");
        prodOrderRelOrderStatusChangeTask.setRunnable(new ProdOrderRelOrderStatusChangeRunnable(prodOrderRelOrderStatusChangeTask));
        schedulerService.startTask(prodOrderRelOrderStatusChangeTask);
    }

    private void regularSettlememtTask(SchedulerService schedulerService) {
        SchedulerTask regularSettlementTask = new SchedulerTask();
        regularSettlementTask.setCron("0 0 1 * * ?");
        regularSettlementTask.setTaskId("2");
        regularSettlementTask.setTaskName("定期理财日结定时任务");
        regularSettlementTask.setRunnable(new RegularSettlementRunnable(regularSettlementTask));
        schedulerService.startTask(regularSettlementTask);
    }

    /**
     * 自动归集
     * @param schedulerService
     */
    private void autoCollectTask(SchedulerService schedulerService) {
        AutoCollectService autoCollectService = ApplicationContextUtil.getBean(AutoCollectService.class);
        SchedulerTask autoCollectTask = new SchedulerTask();
        //获取定时任务执行时间
        //autoCollectTask.setCron(autoCollectService.getCron());
        //设置固定执行时间
        autoCollectTask.setCron("0 5 3 * * ?");
        autoCollectTask.setTaskId("3");
        autoCollectTask.setTaskName("自动归集");
        autoCollectTask.setRunnable(new AutoCollectRunnable(autoCollectTask));
        schedulerService.startTask(autoCollectTask);
    }

    /**
     * 交易状态确认
     * @param schedulerService
     */
    private void transferStatusConfirmTask(SchedulerService schedulerService) {
        SchedulerTask transferStatusConfirmTask = new SchedulerTask();
        //每5分钟轮询一次交易状态
        transferStatusConfirmTask.setCron("0 */5 * * * ?");
        transferStatusConfirmTask.setTaskId("4");
        transferStatusConfirmTask.setTaskName("交易状态确认");
        transferStatusConfirmTask.setRunnable(new TransferStatusConfirmRunnable(transferStatusConfirmTask));
        schedulerService.startTask(transferStatusConfirmTask);
    }

    private void ttySettlementTask(SchedulerService schedulerService) {
        SchedulerTask ttySettlementTask = new SchedulerTask();
        ttySettlementTask.setCron("0 0 2 * * ?");
        ttySettlementTask.setTaskId("5");
        ttySettlementTask.setTaskName("天天盈理财日结定时任务");
        ttySettlementTask.setRunnable(new TtySettlementRunnable(ttySettlementTask));
        schedulerService.startTask(ttySettlementTask);
    }


    /**
     *同步账户资产信息，每30分钟执行一次
     * @param schedulerService
     */
    private void syncAssetsTask(SchedulerService schedulerService) {
        SchedulerTask syncAssetsTask = new SchedulerTask();
        syncAssetsTask.setCron("0 */30 * * * ?");
        syncAssetsTask.setTaskId("6");
        syncAssetsTask.setTaskName("同步账户资产信息");
        syncAssetsTask.setRunnable(new SyncAssetsRunnable(syncAssetsTask));
        schedulerService.startTask(syncAssetsTask);
    }
    private void feeStatisticesTask(SchedulerService schedulerService){
        SchedulerTask feeStatisticesTask = new SchedulerTask();
        feeStatisticesTask.setCron("0 30 0 * * ? ");
        feeStatisticesTask.setTaskId("7");
        feeStatisticesTask.setTaskName("手续费统计定时任务");
        feeStatisticesTask.setRunnable(new FeeStatisticsRunnable(feeStatisticesTask));
        schedulerService.startTask(feeStatisticesTask);
    }
    private void clearTimeoutUserOrder(SchedulerService schedulerService) {
        SchedulerTask prodOrderRelOrderStatusChangeTask= new SchedulerTask();
        prodOrderRelOrderStatusChangeTask.setCron("0 * * * * ?");
        prodOrderRelOrderStatusChangeTask.setTaskId("8");
        prodOrderRelOrderStatusChangeTask.setTaskName("清理超时订单");
        prodOrderRelOrderStatusChangeTask.setRunnable(new UserOrderRunable(prodOrderRelOrderStatusChangeTask));
        schedulerService.startTask(prodOrderRelOrderStatusChangeTask);
    }

}
